package com.szml.user.interceptor;

import com.szml.user.utils.JWTUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author limin
 * @date 2025-06-19 19:18
 * @description
 */
@Component
public class JWTInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {
        //从请求头中获取token
        String token = request.getHeader("Authorization");
        if(token == null || token.isEmpty()){
            throw new RuntimeException("无token信息，请重新登录");
        }
        //验证token
        Long userId = JWTUtils.verify(token);
        if(userId == null){
            throw new RuntimeException("token验证失败");
        }
        return true;
    }
}
